<script lang="ts" setup name="Goods">
import { useRoute } from 'vue-router'
import useStore from '@/store';
import { storeToRefs } from 'pinia';
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import { computed, onUnmounted, ref, watchEffect } from 'vue';
import Message from '@/components/message';
import { CartItem } from '@/types/cart';
const route = useRoute()
const { goods, cart } = useStore()
// goods.getGoodsInfo(route.params.id as string)
const { info } = storeToRefs(goods)
console.log(goods,info);

watchEffect(() => {
  const id = route.params.id
  if (id && route.fullPath === `/goods/${id}`) {
    //先重置信息
    goods.resetGoodsInfo()
    // 在获取信息
    goods.getGoodsInfo(id as string)
  }
})
let skuIdParm = ''
const changeGoodsfn = (skuid: string) => {
  // console.log(skuid);
  skuIdParm = skuid
  // 父组件接收到skuid 去skus列表中查找 更新 商品的现价 原价 库存
  const sku = info.value.skus.find(item => item.id === skuid)
  // console.log(sku);
  if (sku) {
    info.value.price = sku.price
    info.value.oldPrice = sku.oldPrice
    info.value.inventory = sku.inventory
  }
}



//默认选中价格最高规格商品
// const skuid = ref('1379052170887827457')
// const moreskuid = computed(() => {
//   const Arr = info.value.skus?.map(sku => Number(sku.price))
//   // console.log(Arr);

//   // let arr = [12, 223, 44, 56, 785, 34, 857];
//   // arr = [...Arr]
//   let max = Math.max(...Arr);
//   // console.log(max);
//   // console.log(arr);
//   const moreid = info.value.skus?.find(item => {
//     // console.log(Number(item.price));
//     return Number(item.price) === max
//   })
//   // console.log(moreid);
//   skuid.value = moreid!.id
//   return skuid.value
// })

const count = ref(1)
const min = ref(1)
const max = ref(10)
const changeCountfn = (num: number) => {
  console.log('最新的value值', num);
}

//加入购物车
const addCart = async () => {
  if (!skuIdParm) return Message.warning('请选择完整的商品规格')
  // console.log('加入购物车', skuIdParm);
  // await cart.addCart(skuIdParm, count.value)
  // 处理添加商品的attrstext字段
  // 1、拿着skuid去info.skus找对应的sku
  const sku = info.value.skus.find(item => item.id === skuIdParm)
  // console.log(sku);
  // 2、遍历sku的specs 拼接一个字符串
  const arr = sku?.specs.map(item => {
    return `${item.name}:${item.valueName}`
  })
  // console.log(arr?.join(' , '));

  await cart.addCart({
    // 为了本地添加展示数据
    id: info.value.id, // 商品id
    name: info.value.name, // 商品的名字
    picture: info.value.mainPictures[0], // 商品图片
    price: info.value.price, // 商品原价格
    count: count.value, // 商品数量
    skuId: skuIdParm, // 商品skuId
    attrsText: arr?.join(' ')!, // 规格描述
    selected: true, // 是否选中
    nowPrice: info.value.price, // 商品现价
    stock: info.value.inventory, // 商品库存
    isEffective: true, // 商品是否有效
  })
  Message.success('加入购物车成功')
  //解决 跳到下一个商品可以直接加入购物车的bug
  skuIdParm = ''
}
// onUnmounted(() => {
//   // console.log('goods组件销毁了')
//   // console.log(goods)
//   // 当组件销毁的时候，重置goods的数据
//   goods.$reset()
// })
</script>
<template>
  <div class="xtx-goods-page">
    <div class="container">
      <!-- 
        info 默认是空对象  但是 dom 不会等到请求回来后渲染
        v-if 判断 是否有 info
       -->
      <!-- 面包屑 -->
      <Transition name="pop">
        <XtxBread v-if="info.id">
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem :to="`/category/${info.categories[1].id}`">
            {{ info.categories[1].name }}
          </XtxBreadItem>
          <XtxBreadItem :to="`/category/sub/${info.categories[0].id}`">
            {{ info.categories[0].name }}
          </XtxBreadItem>
          <XtxBreadItem>{{ info.name }}</XtxBreadItem>
        </XtxBread>
        <!-- <XtxBread v-else>
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem>{{ info.categories[1].name }}</XtxBreadItem>
          <XtxBreadItem>{{ info.categories[0].name }}</XtxBreadItem>
          <XtxBreadItem>{{ info.name }}</XtxBreadItem>
        </XtxBread> -->
      </Transition>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左侧 -->
        <div class="media">
          <!-- 放大镜图片预览 -->
          <GoodsImage :images="info.mainPictures"></GoodsImage>
          <!-- 商品销量 评价组件-->
          <GoodsSales />
        </div>
        <!-- 右侧 -->
        <div class="spec">
          <!-- goodsname组件  -->
          <GoodsName :goods="info" />
          <!-- 规格组件 -->
          <GoodsSku :goods="info" v-if="info.id" :key="info.id" skuid="1369155862131642369"
            @changeGoods='changeGoodsfn' />
          <!-- skuid 写灵活 -->
          <!-- <GoodsSku :goods="info" v-if="info.id" :key="info.id" :skuid="moreskuid" @changeGoods='changeGoodsfn' /> -->
          <!-- 计数器组件 -->
          <XtxNumbox v-model="count" @changeCount="changeCountfn" :min="min = 1" :max="max = 100"></XtxNumbox>
          <!-- 加入购物车 -->
          <XtxButton @click="addCart" size="large" type="primary" style="margin-top:20px;">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <!-- 商品详情 -->
            <GoodsDetail v-if="info.id" :goods="info" />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <!-- 24热榜+专题推荐 -->
          <div class="goods-aside">
            <!-- 1: '24小时热销榜', -->
            <GoodsHot :type="1" />
            <!-- 2: '周热销榜', -->
            <GoodsHot :type="2" />
            <!-- 3: '总热销榜', -->
            <GoodsHot :type="3" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
